home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 53142 / 53142.xpi / content / sosite.js < prev   
Text File  |  2010-02-04  |  10KB  |  218 lines

  1. /*##########################################################################
  2.     Copyright 2009 Tim Reid
  3.  
  4.     This file is part of the Stack Overflow Reputation Display extension
  5.     for Mozilla Firefox.
  6.  
  7.     Stack Overflow Reputation Display is free software: you can
  8.     redistribute it and/or modify it under the terms of the GNU General
  9.     Public License as published by the Free Software Foundation, either
  10.     version 3 of the License, or (at your option) any later version.
  11.  
  12.     Stack Overflow Reputation Display is distributed in the hope that it
  13.     will be useful, but WITHOUT ANY WARRANTY; without even the implied
  14.     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15.     See the GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public
  18.     License along with Stack Overflow Reputation Display. If not,
  19.     see <http://www.gnu.org/licenses/>.
  20. ##########################################################################*/
  21.  
  22. var SOSite = function (init) {
  23.   if (typeof init == "object") {
  24.     for (var k in init)
  25.       this[k] = init[k];
  26.   } else if (typeof init == "string") {
  27.     for (var i=0; i<SOSite.siteinfo.length; i+=2) {
  28.       var tag = SOSite.siteinfo[i];
  29.       if (init == tag) {
  30.         var info = SOSite.siteinfo[i+1];
  31.         for (var k in info)
  32.           this[k] = info[k];
  33.         this.sitetag = tag;
  34.         break;
  35.       }
  36.     }
  37.   }
  38.  
  39.   return this;
  40. };
  41.  
  42. SOSite.siteinfo = [
  43.                     "so",         {
  44.                                     title:     "Stack Overflow",
  45.                                     uribase:   "http://stackoverflow.com/",
  46. //                                    smallicon: "http://sstatic.net/sf/favicon.ico",
  47.                                     smallicon: "chrome://sorepdisplay/skin/sofavicon.ico",
  48.                                   },
  49.  
  50.                     "sf",         {
  51.                                     title:     "Server Fault",
  52.                                     uribase:   "http://serverfault.com/",
  53.                                     smallicon: "http://sstatic.net/sf/favicon.ico",
  54.                                   },
  55.  
  56.                     "su",         {
  57.                                     title:     "Super User",
  58.                                     uribase:   "http://superuser.com/",
  59.                                     smallicon: "http://sstatic.net/su/favicon.ico",
  60.                                   },
  61.  
  62.                     "meta",       {
  63.                                     title:     "Meta Stack Overflow",
  64.                                     uribase:   "http://meta.stackoverflow.com/",
  65.                                     smallicon: "http://sstatic.net/mso/favicon.ico",
  66.                                   },
  67.  
  68.                     "doctype",    {
  69.                                     title:     "Doctype",
  70.                                     uribase:   "http://doctype.com/",
  71.                                     smallicon: "http://doctype.com/images/favicon.png",
  72.                                   },
  73.  
  74.                     "onstartups", {
  75.                                     title:     "OnStartups Answers",
  76.                                     uribase:   "http://answers.onstartups.com/",
  77.                                     smallicon: "http://answers.onstartups.com/theme/image/theme.favicon.0",
  78.                                   },
  79.  
  80.                     "epic",       {
  81.                                     title:     "EpicAdvice.com",
  82.                                     uribase:   "http://epicadvice.com/",
  83.                                     smallicon: "http://epicadvice.com/theme/image/theme.favicon.0",
  84.                                   },
  85.  
  86.                     "startups",   {
  87.                                     title:     "Startups.com",
  88.                                     uribase:   "http://startups.com/",
  89.                                     smallicon: "http://startups.com/theme/image/theme.favicon.0",
  90.                                   },
  91.  
  92.                     "moms4mom",   {
  93.                                     title:     "moms4mom.com",
  94.                                     uribase:   "http://moms4mom.com/",
  95.                                     smallicon: "http://moms4mom.com/theme/image/theme.favicon.0",
  96.                                   },
  97.  
  98.                     "fogbugz",    {
  99.                                     title:     "FogBugz Knowledge Exchange",
  100.                                     uribase:   "http://fogbugz.stackexchange.com/",
  101.                                     smallicon: "http://fogbugz.stackexchange.com/theme/image/theme.favicon.0",
  102.                                   },
  103.  
  104.                     "mathoverflow", {
  105.                                     title:     "Math Overflow",
  106.                                     uribase:   "http://mathoverflow.net/",
  107.                                     smallicon: "http://mathoverflow.net/theme/image/theme.favicon.0",
  108.                                   },
  109.  
  110.                     "metase",     {
  111.                                     title:     "Meta Stack Exchange",
  112.                                     uribase:   "http://meta.stackexchange.com/",
  113.                                     smallicon: "http://meta.stackexchange.com/theme/image/theme.favicon.0",
  114.                                   },
  115. ];
  116.  
  117. SOSite.prototype = {
  118.   title:        "Stack Overflow",
  119.   uribase:      "http://stackoverflow.com/",
  120.   smallicon:    "chrome://sorepdisplay/skin/sofavicon.ico",
  121.   sitetag:      "so",
  122.  
  123.   pages:        {
  124.                   main:      { description: "main page",
  125.                                uripart:     "",                            },
  126.                   questions: { description: "recent questions",
  127.                                uripart:     "questions?sort=newest",       },
  128.                   profile:   { description: "my profile",
  129.                                uripart:     "users/%u",                    },
  130.                   recent:    { description: "my activity",
  131.                                uripart:     "users/recent/%u",             },
  132.                   activity:  { description: "recent activity on my posts", 
  133.                                uripart:     "users/%u?tab=activity",       },
  134.                   data:      { description: "data source",
  135.                                uripart:     "users/flair/%u.json",         },
  136.                 },
  137.  
  138.   intervals:    [
  139.                   { value:  300, description: "five minutes",    },
  140.                   { value:  900, description: "fifteen minutes", },
  141.                   { value: 1800, description: "half hour",       },
  142.                   { value: 3600, description: "hour",            },
  143.                 ],
  144.  
  145.   badges:       [
  146.                   "gold",
  147.                   "silver",
  148.                   "bronze"
  149.                 ],
  150.  
  151.   bookmarks:    [
  152.                   "main",
  153.                   "questions",
  154.                   "profile",
  155.                   "recent",
  156.                   "activity",
  157.                 ],
  158.  
  159.   uripart:   function (page)       { return this.pages[page].uripart; },
  160.   uri:       function (page, user) {
  161.                                         var part = this.uripart(page);
  162.                                         if (typeof part != "string") return null;
  163.  
  164.                                         var uri = this.uribase + part;
  165.                                         if (uri.match(/%u/) && !user)
  166.                                           return null
  167.  
  168.                                         return uri.replace(/%u/g, user);
  169.                                       },
  170.   getdatauri:   function (user)       { return this.uri("data", user); },
  171.  
  172.   extractre:    {
  173.                   reputation: new RegExp('"reputation":"([\\d,]+)"'),
  174.                   gold:       new RegExp('title=\\\\"(\\d+) gold badges?\\\\"'),
  175.                   silver:     new RegExp('title=\\\\"(\\d+) silver badges?\\\\"'),
  176.                   bronze:     new RegExp('title=\\\\"(\\d+) bronze badges?\\\\"'),
  177.                   avatar:     new RegExp('"gravatarHtml":"\\\\u003cimg src=\\\\"(http:\\/\\/www\\.gravatar\\.com\\/avatar\\/[0-9a-f]+\\?s=50&d=identicon&r=PG)\\\\"'),
  178.                   name:       new RegExp('"displayName":"(.*?)"'),
  179.                 },
  180.  
  181.   extractinfo:  function (text) {
  182.                   var info = {};
  183.                   for (var key in this.extractre) {
  184.                     var m = text.match(this.extractre[key]);
  185.                     if (m) {
  186.                       info[key] = m[1].replace(/&/g, "&");
  187.                       if (info[key].match(/^\d[\d,]*$/))
  188.                         info[key] = parseInt(info[key].replace(/,/g, ""));
  189.                     }
  190.                   }
  191.  
  192.                   return info;
  193.                 },
  194.  
  195.   finduserdata: {
  196.                   xpath:    '//div[@id="hlinks"]/a/@href',
  197.                   extract:  new RegExp (/^\/users\/([0-9]+)\//),
  198.                 },
  199.  
  200.   finduser:     function (doc) {
  201.                   if (!doc ||
  202.                       !doc.URL ||
  203.                       doc.URL.indexOf(this.uribase) != 0)
  204.                     return "";
  205.  
  206.                   var r = doc.evaluate(this.finduserdata.xpath,
  207.                                        doc,
  208.                                        null,
  209.                                        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  210.                                        null);
  211.                   for (var i=0; i<r.snapshotLength; i++) {
  212.                     var node = r.snapshotItem(i);
  213.                     var m = node.nodeValue.match(this.finduserdata.extract);
  214.                     if (m) return m[1];
  215.                   }
  216.                 },
  217. };
  218.